package cn.lutu.domain.search;

import cn.lutu.protocol.search.ArticleSearchVO;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.annotations.HighlightParameters;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ArticleSearchRepository extends CrudRepository<ArticleSearchVO,Long> {

    @Highlight(fields = {
            @HighlightField(name = "title")
    },parameters = @HighlightParameters(
            preTags = "<font color = red>",postTags = "</font>"))
    SearchPage<ArticleSearchVO> findByTitleAndBrief(String title, String brief, Pageable pageable);

    SearchHits<ArticleSearchVO> queryByTitle(String title);

    @Highlight(fields = {
            @HighlightField(name = "title")
    }, parameters = @HighlightParameters(
            preTags = "<font color=red>", postTags = "</font>"))
    @Query("{\n" +
            "    \"bool\": {\n" +
            "      \"should\": [\n" +
            "        {\n" +
            "          \"match\": {\n" +
            "            \"title\": \"?0\"\n" +
            "          }\n" +
            "        },\n" +
            "        {\n" +
            "          \"match\": {\n" +
            "            \"brief\": \"?1\"\n" +
            "          }\n" +
            "        }\n" +
            "      ]\n" +
            "    }\n" +
            "  }")
    SearchPage<ArticleSearchVO> searchByTitleAndBrief(String title, String brief, Pageable pageable);
}
